//
//  UpdateViewController.m
//  FNMenu
//
//  Created by 航汇聚科技 on 2018/9/11.
//  Copyright © 2018年 Yi Zhou. All rights reserved.
//

#import "UpdateViewController.h"
#import "NSDictionary+Safety.h"
#import "NetManager.h"
#import "User.h"
@interface UpdateViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITextField *textField;
@end

@implementation UpdateViewController

#pragma mark - lifeCycle
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.textField becomeFirstResponder];
    if (self.type == UPDATETYPE_Phone) {
        self.titleLabel.text = @"绑定手机";
        self.textField.placeholder = @"输入手机号";
    }
    if (self.type == UPDATETYPE_Name) {
        self.titleLabel.text = @"修改昵称";
        self.textField.placeholder = @"输入你的昵称";
    }
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self updateData];
    return [textField resignFirstResponder];
}
#pragma mark - event response
- (IBAction)cancelAction:(UIButton *)sender {
    [self.textField resignFirstResponder];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)confirmAction:(UIButton *)sender {
    [self updateData];
}
#pragma mark - public
+ (instancetype)updateVCFromStoryBoard {
    return [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"UpdateVC"];
}
#pragma mark - private
- (void)updateData {
    if (self.textField.text.length == 0) {
        return;
    }
    if (self.type == UPDATETYPE_Phone && self.textField.text.length != 11) {
        return;
    }
    [[NetManager defaultNetManager] fetchNetDataWithURLStr:@"home/users/editinfo" params:@{@"uid":[User userID], @"phone_number":self.textField.text} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([[responseObject safeObjectForKey:@"code"] integerValue] == 1) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
#pragma mark - setter
- (void)setType:(UPDATETYPE)type {
    _type = type;
}
@end
